package com.tianqingwl.gsystem.sysuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianqingwl.gsystem.common.enumeration.SearchTypeEnum;
import com.tianqingwl.gsystem.content.service.ContentInfoService;
import com.tianqingwl.gsystem.sysuser.mapper.SysUserMapper;
import com.tianqingwl.gsystem.sysuser.pojo.SysUser;
import com.tianqingwl.gsystem.sysuser.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private ContentInfoService contentInfoService;

    @Override
    public boolean updateSysUserPassword(String username, String password) {
        return sysUserMapper.updateSysUserPassword(username, passwordEncoder.encode(password)) == 1;
    }

    @Override
    public SysUser findSysUserByUsername(String username) {
        return sysUserMapper.findSysUserByUsername(username);
    }

    @Override
    public void listSysUserPage(Page<SysUser> userPage, Integer searchType, String searchStr) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if (searchType != null) {
            SearchTypeEnum type = SearchTypeEnum.getType(searchType.toString());
            switch (type) {
                case EMAIL:
                    queryWrapper.like("email", searchStr);
                    break;
                case PHONE:
                    queryWrapper.like("phone", searchStr);
                    break;
                case SHOW_NAME:
                    queryWrapper.like("showName", searchStr);
                    break;
                case USER_NAME:
                    queryWrapper.like("username", searchStr);
                    break;
            }
        }
        queryWrapper.orderByDesc("createTime");
        sysUserMapper.selectPage(userPage, queryWrapper);
        for (SysUser record : userPage.getRecords()) {
            record.setPassword(null);
        }
    }

    @Override
    public SysUser getUserById(Long userId) {
        if (userId == null) {
            return null;
        }
        SysUser sysUser = sysUserMapper.selectById(userId);
        return sysUser;
    }

    @Transactional
    @Override
    public SysUser save(SysUser sysUser) {
        if (sysUser == null) {
            return null;
        }
        //不更新的字段
        sysUser.setAccountNonExpired(true);
        sysUser.setAccountNonLocked(true);
        sysUser.setCredentialsNonExpired(true);
        sysUser.setEnabled(true);
        Long userId = sysUser.getUserId();
        if (userId == null) {
            sysUser.setCreateTime(new Date());
            sysUser.setPassword(passwordEncoder.encode(sysUser.getPassword()));
            sysUserMapper.insert(sysUser);
            sysUser.setPassword(null);
            return sysUser;
        }
        if (!StringUtils.isEmpty(sysUser.getPassword())) {
            sysUser.setPassword(passwordEncoder.encode(sysUser.getPassword()));
        }

        sysUserMapper.updateById(sysUser);
        contentInfoService.updateUserShowName(sysUser.getUserId(),sysUser.getShowName());
        sysUser.setPassword(null);
        return sysUser;
    }

    @Transactional
    @Override
    public void deleteById(Long userId) {
        sysUserMapper.deleteById(userId);
        sysUserMapper.deleteUserRole(userId);
    }

    @Transactional
    @Override
    public void deleteByIds(List<Long> userIds) {
        for (Long userId : userIds) {
            sysUserMapper.deleteById(userId);
            sysUserMapper.deleteUserRole(userId);
        }
    }

    @Transactional
    @Override
    public void addRole(Long userId, Long[] roleIds) {
        if (userId!=null&&roleIds!=null&&roleIds.length>0){
            for (Long roleId : roleIds) {
                if (roleId!=null){
                    sysUserMapper.insertUserRole(userId,roleId);
                }
            }
        }else{
            throw new RuntimeException("参数不足");
        }
    }
}
